﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    Ingredient ingredient;
    ingredientDAO dao;
    int a=0;
    ArrayList values = new ArrayList();
    ArrayList texts = new ArrayList();
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
        dao = new ingredientDAO();
        ingredient = new Ingredient();
        if (!IsPostBack)
        {
            load_cblNutrient();
        }
        a = CheckboxListSelections(cblNutrient);
        CreateNewTextBox(a, texts, values);
        
        
    }
    

    public void load_cblNutrient()
    {
        DataSet ds = new DataSet();
        ds = dao.getclbNutrient();
        cblNutrient.DataSource = ds.Tables[0];
        cblNutrient.DataTextField = "Nutrient";
        cblNutrient.DataValueField = "NutrientID";
        cblNutrient.DataBind();
        
    }
    public int CheckboxListSelections(System.Web.UI.WebControls.CheckBoxList list)
    {
        //ArrayList values = new ArrayList();
        int x=0;
        
        for (int counter = 0; counter < list.Items.Count; counter++)
        {

            string s = list.Items[counter].Text;
            if (list.Items[counter].Selected)
            {
                
                values.Add(list.Items[counter].Value);
                texts.Add(list.Items[counter].Text);
                
                x = x + 1;
                
            }
            
        }
        return x;
       /* int numSelected = 0;
        foreach (ListItem li in list.Items)
        {
            if (li.Selected)
            {
                
                numSelected = numSelected + 1;
            }
        }
        Response.Write("Total Number Of CheckBoxes Selected:");
        Response.Write(numSelected);
        return numSelected;  */
    }
  /*  protected void AddValue_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < values.Count; i++)
			{
			 Response.Write("Total Number Of CheckBoxes Selected:"+values[i].ToString()+":"+texts[i].ToString());
			}
        
       CreateNewTextBox(a,texts,values);
    }
  */  
    protected void CreateNewTextBox(int number, ArrayList texts,ArrayList values) {
        for (int i = 0; i < number; i++)
        {
            Label lb = new Label();
            lb.Text = texts[i].ToString();
            TextBox tb = new TextBox();
            tb.ID = values[i].ToString();
            tb.Text = "";
            tb.Width = 200;
            Panel1.Controls.Add(lb);
            Panel1.Controls.Add(tb);
            Literal literal = new Literal();
            literal.Text = "<br/>";
            Panel1.Controls.Add(literal);
        }
    }


    /*  protected void cblNutrient_Load(object sender, EventArgs e)
      {
          int y = CheckboxListSelections(cblNutrient);
          Session.Add("NUMNUTRIENT", y);
      }
      protected void cblNutrient_Init(object sender, EventArgs e)
      {
          int y = CheckboxListSelections(cblNutrient);
          Session.Add("NUMNUTRIENT", y);
      }
      protected void cblNutrient_Disposed(object sender, EventArgs e)
      {
        
          int y = CheckboxListSelections(cblNutrient);
          Session.Add("NUMNUTRIENT", y);
      }
   
      protected void cblNutrient_Unload1(object sender, EventArgs e)
      {
          Request.Params.Add("NUTVALUE", CheckboxListSelections(cblNutrient));
      }*/


    protected void submit_Click(object sender, EventArgs e)
    {
        String IngreID = (String)Session["IngredientID"];
        Response.Write(IngreID);
        float[] type = new float[a];
        for (int i = 0; i < a; i++)
        {
           
            String NutriID = values[i].ToString();
            type[i] = float.Parse(((TextBox)Panel1.FindControl(values[i].ToString())).Text);
            dao.addComposition(IngreID, NutriID, type[i]);
            
        }
        Response.Redirect("home.aspx");
        Session.Remove("IngredientID");
        
    }
    protected void reset_Click(object sender, EventArgs e)
    {
        float[] type = new float[a];
        for (int i = 0; i < a; i++)
        {
        string id = "txtID" + i;
        type[i] = float.Parse(((TextBox)Panel1.FindControl(values[i].ToString())).Text);
        Response.Write("Total Number Of CheckBoxes Selected:");
        Response.Write(type[i]);
        }
    }
}